//
//  WishViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/20.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//


#import "WishListViewController.h"
#import "WishTableViewCell.h"
#import "ResponseManager.h"
#import "ResponseManager+MyWish.h"
#import "DetailViewController.h"

static NSString *wishIdentifier = @"wishTableViewCell";
@interface WishListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSoure;

@end

@implementation WishListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的愿望";
    self.tableView.estimatedRowHeight = 30;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self setupBackItem];
    [self.view addSubview:self.tableView];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    NSUserDefaults *userDefauls = [NSUserDefaults standardUserDefaults];
    NSDictionary *userinfo = [userDefauls objectForKey:@"userInfo"];
    [ResponseManager getUserWishInfoUserId:userinfo[@"id"] completionHandler:^(FBResponse *response) {
        self.dataSoure = response.resultValue;
        [self.tableView reloadData];
        
    }];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSoure.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WishTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:wishIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSoure[indexPath.row];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1 * kScreenHeightScale ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    detailVC.model = self.dataSoure[indexPath.row];
    [self.navigationController pushViewController:detailVC animated:YES];
    
}



- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        UINib *nib = [UINib nibWithNibName:@"WishTableViewCell" bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:wishIdentifier];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}

-(NSMutableArray *)dataSoure {
    if (!_dataSoure) {
        _dataSoure = [NSMutableArray array];
    }
    return _dataSoure;
}



@end
